# coding: utf8
from functools import partial
from Tkinter import *
from PIL import ImageTk, Image

import tkFont
import tkMessageBox
import os

buttonBg = '#D5E0EE'
buttonActiveBg = '#E5E35B'


def test_show_info():  
	"""show the software info"""  
	return tkMessageBox.showinfo("About",  
						  """ 
		 开发测试 
	------------------------------------------ 
		version: 1.0 
		author: TenaciousZ 
		Email:  576879814@qq.com 
	------------------------------------------""" )


def test_show_help():  
	"""show help info"""  
	return tkMessageBox.showinfo("help",  
							  """ 
				 Help
		----------------------------------------------------------------- 
		1.help 
		------------------------------------------------------------------""" ) 

class SetTkWindow(object):
	"""设置窗口"""
	def __init__(self, master):
		self.master = master
		self.window_size = '500x300+500+200'	# 窗口大小
		self.ico_path = 'py.ico'	# 图标路径
		self.title = '开发测试'		# 窗口标题

	def init(self):
		"""初始化窗口"""
		self.set_window_size()
		self.set_ico()
		self.set_title()

	def set_window_size(self):
		self.master.geometry(self.window_size)

	def set_ico(self):
		self.master.iconbitmap(self.ico_path)

	def set_title(self):
		self.master.title(self.title)

def button_diy(master, Button):
	"""自定义按钮"""
	buttonFont = tkFont.Font(size=10, weight=tkFont.BOLD)
	myButton = partial(Button, master, bg=buttonBg, padx=20, pady=3, activebackground=buttonActiveBg, font=buttonFont)
	return myButton

def entry_diy(master, Entry):
	"""自定义输入框"""
	entryFont = tkFont.Font(size=12)
	entry = Entry(master, justify="right", font=entryFont)
	return entry

def exit_window(master):  
	"""close the main window"""  
	master.destroy()  


class MenuDIY(object):
	def __init__(self, master):
		self.master = master
		self.menubar = Menu(self.master)

	def init(self):
		pass

	def menu_diy(self):
		optionmenu = Menu(self.menubar, tearoff=1)  
		optionmenu.add_command(label='About', command=self.show_info)  
		optionmenu.add_command(label='Help', command=self.show_help)  
		optionmenu.add_separator()		# 横线
		optionmenu.add_command(label='Exit', command=self.master.destroy)  
		self.menubar.add_cascade(label='Options', menu=optionmenu)

	@staticmethod
	def show_info():  
		"""show the software info"""  
		tkMessageBox.showinfo("About",  
							  """ 
			 Linux Command Manual 
		------------------------------------------ 
			version: 1.0 
			author: Cullen 
			Email:  wangyiyan402@163.com 
		------------------------------------------""" )

	@staticmethod
	def show_help():  
		"""show help info"""  
		tkMessageBox.showinfo("help",  
								  """ 
					 Linux & Autotest Command Manual 
			----------------------------------------------------------------- 
			1.鼠标或键盘箭头移动到所要查看的命令，回车即可在右恻查看命 
			令帮助文档. 
			2.搜索框内可直接输入要查询的命令，回车或点击搜索图标均可. 
			3.如果有新命令文档需要添加，请将文档信息以txt格式，编码选择 
			utf-8保存到doc文件夹下. 
			------------------------------------------------------------------""" ) 


class FrameDIY(object):

	def __init__(self):  
		self.message_ft = tkFont.Font(family="Arial", size=12)
		self.doc_path = '%s%sdoc%s' % (os.getcwd(), os.sep , os.sep)
		self.left_frame = ''		# 左边表单
		self.right_frame = ''		# 右边表单
		self.text_frame = ''		# 右下角文本表单
		self.listbox = ''	# 左边表单, 列表框
		self.entry = ''		# 右边表单, 输入框
		self.image_path = 'wifi.jpg'
		self.image = '' 	# 右边表单, 按钮图片
		self.textarea = ''  # 右下角文本表单, 文本输出区域

	def init(self):
		pass

	def left_frame_diy(self):
		"""自定义左边表单"""
		self.left_frame = Frame()
		self.listbox = Listbox(self.left_frame, bg="#D6D6D6", 
								selectborderwidth=1, 
								selectbackground="#1979CA",  
								font=self.message_ft, 
								height=20, 
								width=30)
		scrollbar = Scrollbar(self.left_frame, orient=VERTICAL, bg='green')  
		scrollbar.config(command=self.listbox.yview)  
		scrollbar.pack(side=LEFT, fill=Y)  
		self.listbox.config(yscrollcommand=scrollbar.set)  
		self.listbox.pack(side=RIGHT, fill=BOTH, expand=1)

	def right_frame_diy(self): 
		"""自定义右下角文本表单"""
		self.right_frame = Frame()
		self.image = ImageTk.PhotoImage(file=self.image_path)
		button = Button(self.right_frame, image=self.image, command=self.get_entry_input)  
		button.pack(side=RIGHT, fill=X)  
  
		self.entry = Entry(self.right_frame, bd=2, width=40, justify=RIGHT)  
		self.entry.pack(side=RIGHT, fill=X)  
		self.entry.bind('<Return>', self.get_input)

	def text_frame_diy(self):
		"""自定义右边表单"""
		self.text_frame = Frame()  
		scrollbar = Scrollbar(self.text_frame, orient=VERTICAL)  
		self.textarea = Text(self.text_frame, bg='#CCFFCC', width=90, height=30)

		scrollbar.config(command=self.textarea.yview)  
		scrollbar.pack(side=RIGHT, fill=Y)  
		self.textarea.config(yscrollcommand=scrollbar.set)  
		self.textarea.pack(side=LEFT, fill=BOTH, expand=1)

	def get_input(self, event):  
		input = self.entry.get().strip()  
		self.entry.delete(0, END)  
		self.find_input(input)  
  
	def get_entry_input(self):  
		#print os.getcwd()  
		input = self.entry.get().strip()  
		self.find_input(input)

	def find_input(self, input):  
		file_list = get_file_list(self.doc_path)
		input += '.txt'
		file_path = '%s%s' % (self.doc_path, input)
  
		if input not in file_list:  
			tkMessageBox.showwarning("提示", "Command : %s not found!" % input)  
			return  
		index = file_list.index(input)  
		self.listbox.selection_clear(0, END)  
		self.listbox.selection_set(index)  
  
		self.textarea.config(state=NORMAL)  
		self.textarea.delete(1.0, END)  
  
		try:
			with open(file_path) as f:  
				for item in f:  
					self.textarea.insert(END, item)  
		except IOError, e:  
			#print "open cmd info error: %s" % e  
			tkMessageBox.showwarning("提示", "open cmd %s: %s" % (input, e))  
			self.entry.delete(0, END)

		self.textarea.config(state=DISABLED) 
  
	def show_cmd_mean(self, event):  
		self.textarea.config(state=NORMAL)  
		self.textarea.delete(1.0, END)  
  
		self.active = event.widget.get(ACTIVE) 
		try:  
			file_path = self.doc_path + self.active + '.txt'  
			with open(file_path, 'r') as f:  
				for item in f:  
					self.textarea.insert(END, item)  
		except IOError, e:  
			tkMessageBox.showwarning('提示', "open cmd info error: %s" % e)  
  
		self.textarea.config(state=DISABLED)

	def test_show(self, input):
		tkMessageBox.showwarning('发现内容', "%s" % input)  


def get_file_list(dir):
	"""获取目录下的txt文件"""
	if not os.path.exists(dir):  
		print "Path Error: No such file or directory"  
		tkMessageBox.showwarning("提示", "Directory : %s not found!" % dir)  
  
	file_list = os.listdir(dir)
	result = [i for i in file_list if i.endswith('.txt')]
	result.sort()
	return result 
 